
var _bank = [];

var Pay = {
	collapse: function (cc) {
		$("#label_zh").toggle();
		$("#label_gh").toggle();
	
		if ($(cc).hasClass("ui-icon-circle-triangle-w")) {
			$(cc).removeClass("ui-icon-circle-triangle-w").addClass("ui-icon-circle-triangle-s");			
		} else {
			$(cc).removeClass("ui-icon-circle-triangle-s").addClass("ui-icon-circle-triangle-w");
		}
		
		return false;
	},
	
	emp: function (a) {
		var body = {};
		body.q = a;
		
		$.getJSON('finance.php?do=PaymentMy.ajax_bank1', body, function(json) {
			_bank = json;
			
			Pay.zh_x();
			Pay.gh_x();
		});
	},
		
	zh_x: function () {
		$select = $("#zh_bank");		
		$select.empty();
		
		var count = 0;
		$.each(_bank, function(k, v) {
			var a;
			if (v.id == '-') {
				a = v.name;
			} else {
				var s = v.code.replace(/\s+/g, "");
				a = v.name + "/" + s.right(4);				
			}
			// alert($.toJSON(v));
			
	    	var item = new Option(a, v.id);
	    	$select[0].options.add(item);
	    	
	    	if (count == 1) {
	    		Pay.zh(v.id, false);
	    	}
	    	
	    	count++;
	    });
		
		$select.change(function() {
			var id = $(this).val();
			Pay.zh(id, true);
		});		
		
		// $("#label_zh").removeClass("ui-icon-circle-triangle-w").addClass("ui-icon-circle-triangle-s");		
	},
	
	zh: function (id, select) {
		var url = 'finance.php?do=Bank.ajax_id&q=' + id;
		$.getJSON(url, function(json) {
			var zh_is = Pay.zh_is();
			if (select || zh_is) {
				$("#zh_x_bank").val(json.bank + json.branch);
				$("#zh_x_name").val(json.name);
				$("#zh_x_code").val(json.code);
			}
		});
	},
	
	zh_is: function() {
		if ($("#zh_x_bank").val().trim() != "") {
			return false;
		}
		 
		if ($("#zh_x_name").val().trim() != "") {
			return false;
		} 
		
		if ($("#zh_x_code").val().trim() != "") {
			return false;
		}
		
		return true;
	},
	
	gh_x: function () {
		$select = $("#gh_bank");		
		$select.empty();
		
		var count = 0;
		$.each(_bank, function(k, v) {
			var a;
			if (v.id == '-') {
				a = v.name;
			} else {
				var s = v.code.replace(/\s+/g, "");
				a = v.name + "/" + s.right(4);				
			}
			// alert($.toJSON(v));
			
	    	var item = new Option(a, v.id);
	    	$select[0].options.add(item);
	    	
	    	if (count == 1) {
	    		Pay.gh(v.id, false);
	    	}
	    	
	    	count++;
	    });
		
		$select.change(function() {
			var id = $(this).val();
			Pay.gh(id, true);
		});		
		
		// $("#payment_x").removeClass("ui-icon-circle-triangle-w").addClass("ui-icon-circle-triangle-s");		
	},
	
	gh: function (id, select) {
		var url = 'finance.php?do=Bank.ajax_id&q=' + id;
		$.getJSON(url, function(json) {
			var gh_is = Pay.gh_is();
			if (select || gh_is) {
				$("#gh_x_bank").val(json.bank + json.branch);
				$("#gh_x_name").val(json.name);
				$("#gh_x_code").val(json.code);
			}
		});
	},
	
	gh_is: function() {
		if ($("#gh_x_bank").val().trim() != "") {
			return false;
		}
		 
		if ($("#gh_x_name").val().trim() != "") {
			return false;
		} 
		
		if ($("#gh_x_code").val().trim() != "") {
			return false;
		}
		
		return true;
	}
};
